package com.example.javalab.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Demo {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        Map<String, Object> map = new HashMap<>();
        map.put("age", 25);
        map.put("name", "yitian");
        map.put("interests", new String[]{"pc games", "music"});

        String text = mapper.writeValueAsString(map);
        System.out.println(text);

        Map<String, Object> map2 = mapper.readValue(text, new TypeReference<Map<String, Object>>() {
        });
        System.out.println(map2);

        JsonNode root = mapper.readTree(text);
        String name = root.get("name").asText();
        int age = root.get("age").asInt();

        System.out.println("name:" + name + " age:" + age);
    }

    // {"test":[{"volume":234,"driver":[{"ip":"192.168.1.1"},{"ip":"192.168.1.2"},{"ip":"192.168.1.3"}]},{"volume":234123,"driver":[{"ip":"192.168.1.1"},{"ip":"192.168.1.2"},{"ip":"192.168.1.3"}]}]}
    synchronized public static boolean existInMultiPathConfigFile(long volumeId) {
            try {
        ObjectMapper objMap = new ObjectMapper();
        System.out.println("array size is 111111111111111111");
        JsonNode root = objMap.readTree(new File("/home/test/multipaths.conf"));
        System.out.println("array size is 2222222222222222222222");
        JsonNode node = root.path("test");
        System.out.println("array size is " + node.size());

        for (int i = 0; i < node.size(); i++) {
            Long volume = node.path(i).path("volume").asLong();
            if (volume.equals(volumeId)) {
                System.out.println("exist connection in multipaths.conf.");
                return true;
            }
        }
    } catch (Exception ex) {
        System.out.println("error to read config file.");
        return false;
    }
        System.out.println("nonexist@@@@@@@@@@@@@@@@@@@@.");
        return false;
}

    //写
    synchronized public static boolean writeMultiPathConfigFile(long volumeId, List<String> ips) {
        try {
            ObjectMapper objMap = new ObjectMapper();
            JsonNode root = objMap.readTree(new File("/home/test/multipaths.conf"));
            ArrayNode node = (ArrayNode)root.path("test");
            ObjectNode newNode = objMap.createObjectNode();
            newNode.put("volume", volumeId);
            ArrayNode ipArray = objMap.createArrayNode();
            for (String ip : ips) {
                ObjectNode ipNode = objMap.createObjectNode();
                ipNode.put("ip", ip);
                ipArray.add(ipNode);
            }
            newNode.put("driver", ipArray);
            node.add(newNode);
            OutputStream outputStream = new FileOutputStream(new File("/home/test/multipaths.conf"));
            objMap.writeValue(outputStream, root);
        } catch (Exception ex) {
            System.out.println("error to read config file.");
            return true;
        }
        return false;
    }
    //删除
    synchronized public static boolean eraseMultiPathConfigFile(long volumeId) {
        try {
            ObjectMapper objMap = new ObjectMapper();
            JsonNode root = objMap.readTree(new File("/home/test/multipaths.conf"));
            JsonNode node = root.path("test");
            Iterator<JsonNode> iter = node.iterator();
            while (iter.hasNext()) {
                JsonNode next = iter.next();
                System.out.println("erase the match line." + next.get("volume"));
                if (next.get("volume").asLong() == volumeId) {
                    System.out.println("erase the match line.******************");
                    iter.remove();
                }
            }

            OutputStream outputStream = new FileOutputStream(new File("/home/test/multipaths.conf"));
            objMap.writeValue(outputStream, root);
        } catch (Exception ex) {
            System.out.println("error to read config file.");
            return true;
        }
        return false;
    }
}
